package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.exception.CustomException;
import cn.swust.designpatternlearningplatform.common.utils.PdfGenerateUtil;
import cn.swust.designpatternlearningplatform.domain.bo.FileBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/testtest")
public class TestTestTest {
    private final PdfGenerateUtil pdfGenerateUtil;

    @Authentication
    @GetMapping("/export")
    public void generatePdfZipTest(HttpServletResponse response) {
        try {
            response.reset();
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("111.zip", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("不支持UTF-8编码");
            throw new CustomException("系统问题，请联系后台人员");
        }
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            //属性创建
            List<FileBo> fileBos = new ArrayList<>();
            FileBo fileBo = new FileBo();
            fileBo.setFileName("1");
            Map<String, Object> properties = new HashMap<>();
            properties.put("userName", "小明");
            fileBo.setProperties(properties);
            fileBos.add(fileBo);
//            byte[] bytes = pdfGenerateUtil.generatePdfZip(fileBos, "test.ftl");
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new CustomException("导出失败");
        }
    }

    public TestTestTest(PdfGenerateUtil pdfGenerateUtil) {
        this.pdfGenerateUtil = pdfGenerateUtil;
    }
}
